package zhouzhouyongche.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

import zhouzhouyongche.client_util.Json;

public class BaseAction extends ActionSupport {

	private int page;
	private int rows;

	protected void writeJson(int total, List list){
		try {
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("rows", list);
		maps.put("total", total);
		String json = JSON.toJSONString(maps); 
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);
		ServletActionContext.getResponse().getWriter().flush();
		
		} catch (IOException e) {
			//logger.debug(ExceptionUtil.getExceptionMessage(e));
		}
	}
	
	protected void writeJson(int result){
		try {
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("rows", result);
		String json = JSON.toJSONString(maps); 
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);
		ServletActionContext.getResponse().getWriter().flush();
		
		} catch (IOException e) {
			//logger.debug(ExceptionUtil.getExceptionMessage(e));
		}
	}
	
	protected void writeJson(List list){
		try {
		String json = JSON.toJSONString(list); 
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);
		ServletActionContext.getResponse().getWriter().flush();
		
		} catch (IOException e) {
			//logger.debug(ExceptionUtil.getExceptionMessage(e));
		}
	}
	
	protected void writeJson(Json jsonObj) {
		try {
			String json = JSON.toJSONString(jsonObj); 
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			
			} catch (IOException e) {
				//logger.debug(ExceptionUtil.getExceptionMessage(e));
			}
	}
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
}
